package com.atguigu.common.exception;

import com.atguigu.common.result.Result;
import com.atguigu.common.result.ResultCodeEnum;
import io.minio.errors.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.io.IOException;
import java.rmi.ServerException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.HashMap;

/**
 * ClassName: GlobalException
 * Package: com.atguigu.common.exception
 * Description: 全局异常处理类
 *
 * @Author 梅超凡
 * @Create 2024/11/13 10:44
 * @Version 1.0
 */
@RestControllerAdvice
@Slf4j
public class GlobalException {

    //参数校验异常
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result<HashMap<Object,Object>> methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e){
        //获取错误信息结果集
        BindingResult bindingResult = e.getBindingResult();
        //创建Map映射封装错误信息
        HashMap<Object,Object> objectHashMap = new HashMap<>();
        //遍历存入Map内
        bindingResult.getFieldErrors().forEach(
                fieldError -> {
                    //拿到并封装错误信息
                    objectHashMap.put(fieldError.getField(),fieldError.getDefaultMessage());
                }
        );
        //返回错误结果
        return Result.error(500,"参数校验错误",objectHashMap);
    }

    //自定义业务异常处理
    @ExceptionHandler(SelfBusException.class)
    public Result<SelfBusException> selfBusExceptionHandler(SelfBusException e){
        return Result.error(e.getCode(),e.getMessage(),null);
    }

    //IllegalArgumentException：参数异常getUserRolesListByUserId
    @ExceptionHandler(IllegalArgumentException.class)
    public Result<String[]> illegalArgumentExceptionHandler(IllegalArgumentException e){
        String[] split = e.getMessage().split(";");
        return Result.build(ResultCodeEnum.PARAM_ERROR,split);
    }

    //IllegalStateException:非法状态异常
    @ExceptionHandler(IllegalStateException.class)
    public Result<String[]> illegalStateExceptionHandler(IllegalStateException e){
        String[] split = e.getMessage().split(";");
        return Result.error(500, "非法状态异常:"+e.getCause(),split);
    }
    //ClassCastException:强制类型转换异常
    @ExceptionHandler(ClassCastException.class)
    public Result<String[]> classCastExceptionHandler(ClassCastException e){
        String[] split = e.getMessage().split(";");
        return Result.error(500, "强制类型转换异常:"+e.getCause(),split);
    }

    //HttpMessageNotReadableException:请求参数解析异常
    @ExceptionHandler(HttpMessageNotReadableException.class)
    public Result<String[]> httpMessageNotReadableExceptionHandler(HttpMessageNotReadableException e){
        String[] split = e.getMessage().split(";");
        return Result.error(500, "请求参数解析异常:"+e.getCause(),split);
    }
    //ServerException:服务异常
    @ExceptionHandler(ServerException.class)
    public Result<String[]> serverExceptionHandler(ServerException e){
        String[] split = e.getMessage().split(";");
        return Result.error(500, "服务异常:"+e.getCause(),split);
    }
    //InsufficientDataException:缺少数据异常
    @ExceptionHandler(InsufficientDataException.class)
    public Result<String[]> insufficientDataExceptionHandler(InsufficientDataException e){
        String[] split = e.getMessage().split(";");
        return Result.error(500, "缺少数据异常:"+e.getCause(),split);
    }
    //ErrorResponseException:响应异常
    @ExceptionHandler(ErrorResponseException.class)
    public Result<String[]> errorResponseExceptionHandler(ErrorResponseException e){
        String[] split = e.getMessage().split(";");
        return Result.error(500, "响应异常:"+e.getCause(),split);
    }
    //IOException:IO异常
    @ExceptionHandler(IOException.class)
    public Result<String[]> ioExceptionHandler(IOException e){
        String[] split = e.getMessage().split(";");
        return Result.error(500, "IO异常:"+e.getCause(),split);
    }
    //NoSuchAlgorithmException:算法异常
    @ExceptionHandler(NoSuchAlgorithmException.class)
    public Result<String[]> noSuchAlgorithmExceptionHandler(NoSuchAlgorithmException e){
        String[] split = e.getMessage().split(";");
        return Result.error(500, "算法异常:"+e.getCause(),split);
    }
    //InvalidKeyException:密钥异常
    @ExceptionHandler(InvalidKeyException.class)
    public Result<String[]> invalidKeyExceptionHandler(InvalidKeyException e){
        String[] split = e.getMessage().split(";");
        return Result.error(500, "密钥异常:"+e.getCause(),split);
    }
    //InvalidResponseException:无效响应异常
    @ExceptionHandler(InvalidResponseException.class)
    public Result<String[]> invalidResponseExceptionHandler(InvalidResponseException e){
        String[] split = e.getMessage().split(";");
        return Result.error(500, "无效响应异常:"+e.getCause(),split);
    }
    //XmlParserException:xml解析异常
    @ExceptionHandler(XmlParserException.class)
    public Result<String[]> xmlParserExceptionHandler(XmlParserException e){
        String[] split = e.getMessage().split(";");
        return Result.error(500, "xml解析异常:"+e.getCause(),split);
    }
    //InternalException:内部异常
    @ExceptionHandler(InternalException.class)
    public Result<String[]> internalExceptionHandler(InternalException e){
        String[] split = e.getMessage().split(";");
        return Result.error(500, "Minio内部异常:"+e.getCause(),split);
    }

    @ExceptionHandler(DataIntegrityViolationException.class)
    public Result<String[]> dataIntegrityViolationExceptionHandler(DataIntegrityViolationException e){
        String[] split = e.getMessage().split(";");
        Throwable cause = e.getCause();
        return Result.error(500,"参数长度超过规定值:"+cause, split);
    }


}
